﻿<script lang="ts" name="outboundmanagement" setup>
import { ref, reactive, onMounted } from "vue";
import { ElMessage } from "element-plus";
import type { FormRules } from "element-plus";
import { formatDate } from '/@/utils/formatTime';
import { useOutboundmanagementApi } from '/@/api/InboundManagement/outboundmanagement';

//父级传递来的函数，用于回调
const emit = defineEmits(["reloadTable"]);
const outboundmanagementApi = useOutboundmanagementApi();
const ruleFormRef = ref();

const state = reactive({
	title: '',
	loading: false,
	showDialog: false,
	ruleForm: {} as any,
	stores: {},
	dropdownData: {} as any,
});

// 自行添加其他规则
const rules = ref<FormRules>({
});

// 页面加载时
onMounted(async () => {
  const data = await outboundmanagementApi.getDropdownData(false).then(res => res.data.result) ?? {};
  state.dropdownData.warehouseName = data.warehouseName ?? [];
  state.dropdownData.associatedNumber = data.associatedNumber ?? [];
  state.dropdownData.clientName = data.clientName ?? [];
  state.dropdownData.attn = data.attn ?? [];
  state.dropdownData.manufactureNumberPeople = data.manufactureNumberPeople ?? [];
  state.dropdownData.departmentId = data.departmentId ?? [];
});

// 打开弹窗
const openDialog = async (row: any, title: string) => {
	state.title = title;
	row = row ?? {  };
	state.ruleForm = row.id ? await outboundmanagementApi.detail(row.id).then(res => res.data.result) : JSON.parse(JSON.stringify(row));
	state.showDialog = true;
};

// 关闭弹窗
const closeDialog = () => {
	emit("reloadTable");
	state.showDialog = false;
};

// 提交
const submit = async () => {
	ruleFormRef.value.validate(async (isValid: boolean, fields?: any) => {
		if (isValid) {
			let values = state.ruleForm;
			await outboundmanagementApi[state.ruleForm.id ? 'update' : 'add'](values);
			closeDialog();
		} else {
			ElMessage({
				message: `表单有${Object.keys(fields).length}处验证失败，请修改后再提交`,
				type: "error",
			});
		}
	});
};

//将属性或者函数暴露给父组件
defineExpose({ openDialog });
</script>
<template>
	<div class="outboundmanagement-container">
		<el-dialog v-model="state.showDialog" :width="800" draggable :close-on-click-modal="false">
			<template #header>
				<div style="color: #fff">
					<span>{{ state.title }}</span>
				</div>
			</template>
			<el-form :model="state.ruleForm" ref="ruleFormRef" label-width="auto" :rules="rules">
				<el-row :gutter="35">
					<el-form-item v-show="false">
						<el-input v-model="state.ruleForm.id" />
					</el-form-item>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20" >
						<el-form-item label="出库单号" prop="outboundNumber">
							<el-input v-model="state.ruleForm.outboundNumber" placeholder="请输入出库单号" maxlength="32" show-word-limit clearable />
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20" >
						<el-form-item label="出库类型" prop="outboundType">
							<el-input v-model="state.ruleForm.outboundType" placeholder="请输入出库类型" maxlength="32" show-word-limit clearable />
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20" >
						<el-form-item label="仓库名称" prop="warehouseName">
							<el-select clearable filterable v-model="state.ruleForm.warehouseName" placeholder="请选择仓库名称">
								<el-option v-for="(item,index) in state.dropdownData.warehouseName" :key="index" :value="item.value" :label="item.label" />
							</el-select>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20" >
						<el-form-item label="出库日期" prop="outboundDate">
							<el-date-picker v-model="state.ruleForm.outboundDate" type="date" placeholder="出库日期" />
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20" >
						<el-form-item label="关联单号" prop="associatedNumber">
							<el-select clearable filterable v-model="state.ruleForm.associatedNumber" placeholder="请选择关联单号">
								<el-option v-for="(item,index) in state.dropdownData.associatedNumber" :key="index" :value="item.value" :label="item.label" />
							</el-select>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20" >
						<el-form-item label="客户名称" prop="clientName">
							<el-select clearable filterable v-model="state.ruleForm.clientName" placeholder="请选择客户名称">
								<el-option v-for="(item,index) in state.dropdownData.clientName" :key="index" :value="item.value" :label="item.label" />
							</el-select>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20" >
						<el-form-item label="收货人" prop="consignee">
							<el-input v-model="state.ruleForm.consignee" placeholder="请输入收货人" maxlength="32" show-word-limit clearable />
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20" >
						<el-form-item label="联系方式" prop="consigneePhone">
							<el-input v-model="state.ruleForm.consigneePhone" placeholder="请输入联系方式" maxlength="32" show-word-limit clearable />
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20" >
						<el-form-item label="经办人" prop="attn">
							<el-select clearable filterable v-model="state.ruleForm.attn" placeholder="请选择经办人">
								<el-option v-for="(item,index) in state.dropdownData.attn" :key="index" :value="item.value" :label="item.label" />
							</el-select>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20" >
						<el-form-item label="备注" prop="desc">
							<el-input v-model="state.ruleForm.desc" placeholder="请输入备注" maxlength="300" type="textarea" show-word-limit clearable />
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20" >
						<el-form-item label="状态" prop="status">
							<el-switch v-model="state.ruleForm.status" active-text="是" inactive-text="否" />
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20" >
						<el-form-item label="出库货品" prop="outboundGoods">
							<el-input v-model="state.ruleForm.outboundGoods" placeholder="请输入出库货品" maxlength="32" show-word-limit clearable />
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20" >
						<el-form-item label="出库数量" prop="outboundNum">
							<el-input-number v-model="state.ruleForm.outboundNum" placeholder="请输入出库数量" clearable />
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20" >
						<el-form-item label="出库金额" prop="outboundPrice">
							<el-input-number v-model="state.ruleForm.outboundPrice" placeholder="请输入出库金额" clearable />
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20" >
						<el-form-item label="制单人" prop="manufactureNumberPeople">
							<el-select clearable filterable v-model="state.ruleForm.manufactureNumberPeople" placeholder="请选择制单人">
								<el-option v-for="(item,index) in state.dropdownData.manufactureNumberPeople" :key="index" :value="item.value" :label="item.label" />
							</el-select>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20" >
						<el-form-item label="所在部门" prop="departmentId">
							<el-select clearable filterable v-model="state.ruleForm.departmentId" placeholder="请选择所在部门">
								<el-option v-for="(item,index) in state.dropdownData.departmentId" :key="index" :value="item.value" :label="item.label" />
							</el-select>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20" >
						<el-form-item label="制单时间" prop="manufactureNumberDate">
							<el-date-picker v-model="state.ruleForm.manufactureNumberDate" type="date" placeholder="制单时间" />
						</el-form-item>
					</el-col>
				</el-row>
			</el-form>
			<template #footer>
				<span class="dialog-footer">
					<el-button @click="() => state.showDialog = false">取 消</el-button>
					<el-button @click="submit" type="primary" v-reclick="1000">确 定</el-button>
				</span>
			</template>
		</el-dialog>
	</div>
</template>
<style lang="scss" scoped>
:deep(.el-select), :deep(.el-input-number) {
  width: 100%;
}
</style>